#ifndef _PVE_GAME_H_
#define _PVE_GAME_H_

#include <ncurses.h>
#include <string>
#include <vector>
#include <memory>
#include <optional>

#include "snake.h"

enum class PvEGameResult {
    ONGOING,
    PLAYER_WIN,
    AI_WIN,
    DRAW
};

enum class PvEDifficulty {
    EASY = 0,
    MEDIUM = 1,
    HARD = 2
};

class pveGame
{
public:
    pveGame();
    pveGame(int skinIndex);
    ~pveGame();
    
    void createInformationBoard();
    void renderInformationBoard() const;
    void createGameBoard();
    void renderGameBoard() const;
    void createInstructionBoard();
    void renderInstructionBoard() const;
    void renderLeaderBoard() const;
    bool renderRestartMenu() const;
    
    void renderPoints() const;
    void renderDifficulty() const;
    void initializeGame();
    void createRamdonFood();
    void renderFood() const;
    void renderSnakes() const;
    
    PvEGameResult checkCollisions();
    void renderBoards() const;
    void adjustDelay();
    void runGame();
    void startGame();
    
    // AI相关方法
    void updateAI();
    Direction calculateAIDirection();
    bool isValidMove(Snake* snake, Direction dir) const;
    int calculateDistance(const SnakeBody& pos1, const SnakeBody& pos2) const;
    
    // 文件操作
    bool readLeaderBoard();
    bool updateLeaderBoard();
    bool writeLeaderBoard();
    
    // 暂停相关方法
    void togglePause();
    void renderPauseMenu() const;
    bool isPaused() const;
    
    // 皮肤相关方法
    void setSkinSymbol(int skinIndex);
    char getSkinSymbol(int skinIndex) const;
    
    // 新增难度选择相关方法
    void selectDifficulty();
    void setDifficulty(PvEDifficulty difficulty);
    std::string getDifficultyName() const;
    
    // 显示规则文件的方法
    void showRuleFile();

private:
    // 窗口相关
    int mScreenWidth;
    int mScreenHeight;
    int mGameBoardWidth;
    int mGameBoardHeight;
    const int mInformationHeight = 6;
    const int mInstructionWidth = 18;
    std::vector<WINDOW *> mWindows;
    
    // 游戏状态
    PvEGameResult mGameResult;
    bool mIsPaused = false;
    
    // 蛇相关
    const int mInitialSnakeLength = 2;
    const char mPlayerSnakeSymbol = '@';
    const char mAISnakeSymbol = '>';
    char mCurrentPlayerSnakeSymbol;
    int mCurrentSkinIndex = 0;
    
    std::unique_ptr<Snake> mPlayerSnake;
    std::unique_ptr<Snake> mAISnake;
    
    // 食物相关
    SnakeBody mFood;
    const char mFoodSymbol = '#';
    
    // 分数相关
    int mPlayerPoints = 0;
    int mAIPoints = 0;
    int mDifficulty = 0;
    int mBaseDelay = 100;
    int mDelay;
    
    // 排行榜相关
    const std::string mRecordBoardFilePath = "record_pve.dat";
    std::vector<int> mLeaderBoard;
    const int mNumLeaders = 3;
    
    // 游戏计数器
    long long mFrameCounter = 0;
    
    // AI相关
    int mAIUpdateInterval = 3; // AI每3帧更新一次决策
    Direction mCurrentAIDirection = Direction::Up;
    
    // AI难度参数
    float mAIAggressiveness = 0.7f; // 攻击性强度 (0-1)
    int mAIReactionTime = 2; // AI反应时间(帧数)
    
    // 难度系统
    PvEDifficulty mSelectedDifficulty = PvEDifficulty::MEDIUM;
    float mDifficultyAggressiveness = 0.7f; // 基于难度的攻击性
    int mDifficultyReactionTime = 2; // 基于难度的反应时间
    int mDifficultyAIUpdateInterval = 3; // 基于难度的AI更新间隔
};

#endif
